import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { erpPriceInputFormatter } from '@vben/utils';

import { z } from '#/adapter/form';
import { getAccountSimpleList } from '#/api/erp/finance/account';
import { getProductSimpleList } from '#/api/erp/product/product';
import { getCustomerSimpleList } from '#/api/erp/sale/customer';
import { getSimpleUserList } from '#/api/system/user';
import { getRangePickerDefaultProps } from '#/utils';

import { $t } from '#/locales';

/** 表单的配置项 */
export function useFormSchema(formType: string): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'no',
      label: $t('views.erp.sale.order.no'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.common.generateBySystem'),
        disabled: true,
      },
    },
    {
      fieldName: 'orderTime',
      label: $t('views.erp.sale.order.orderTime'),
      component: 'DatePicker',
      componentProps: {
        placeholder: $t('views.erp.sale.order.orderTimePlaceholder'),
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
      rules: 'required',
    },
    {   
      label: $t('views.erp.common.customer'),
      fieldName: 'customerId',
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.customerPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getCustomerSimpleList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
      },
      rules: 'required',
    },
    {
      fieldName: 'saleUserId',
      label: $t('views.erp.sale.order.saleUser'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.sale.order.saleUserPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getSimpleUserList,
        fieldNames: {
          label: 'nickname',
          value: 'id',
        },
      },
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
        autoSize: { minRows: 1, maxRows: 1 },
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-2',
    },
    {
      fieldName: 'fileUrl',
      label: $t('views.erp.common.fileUrl'),
      component: 'FileUpload',
      componentProps: {
        maxNumber: 1,
        maxSize: 10,
        accept: [
          'pdf',
          'doc',
          'docx',
          'xls',
          'xlsx',
          'txt',
          'jpg',
          'jpeg',
          'png',
        ],
        showDescription: formType !== 'detail',
        disabled: formType === 'detail',
      },
      formItemClass: 'col-span-3',
    },
    {
      fieldName: 'items',
      label: $t('views.erp.common.productItem.title'),
      component: 'Input',
      formItemClass: 'col-span-3',
    },
    {
      fieldName: 'discountPercent',
      label: $t('views.erp.common.discountPercent'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.common.discountPercentPlaceholder'),
        min: 0,
        max: 100,
        precision: 2,
      },
      rules: z.number().min(0).optional(),
    },
    {
      fieldName: 'discountPrice',
      label: $t('views.erp.common.discountPrice'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.common.discountPricePlaceholder'),
        precision: 2,
        formatter: erpPriceInputFormatter,
        disabled: true,
      },
    },
    {
      fieldName: 'totalPrice',
      label: $t('views.erp.common.discountedPrice'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.common.discountedPricePlaceholder'),
        precision: 2,
        formatter: erpPriceInputFormatter,
        disabled: true,
      },
    },
    {
      fieldName: 'accountId',
      label: $t('views.erp.common.account'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.accountPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getAccountSimpleList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
      },
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.erp.sale.order.depositPricePlaceholder'),
        precision: 2,
        min: 0,
      },
      fieldName: 'depositPrice',
      label: $t('views.erp.sale.order.depositPrice'),
      rules: z.number().min(0).optional(),
    },
  ];
}

/** 表单的明细表格列 */
export function useFormItemColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'seq', title: $t('views.common.seq'), minWidth: 80, fixed: 'left' },
    {
      field: 'productId',
      title: $t('views.erp.common.productItem.name'),
      minWidth: 200,
      slots: { default: 'productId' },
    },
    {
      field: 'stockCount',
      title: $t('views.erp.common.productItem.stockCount'),
      minWidth: 90,
      formatter: 'formatAmount3',
    },
    {
      field: 'productBarCode',
      title: $t('views.erp.common.productItem.productBarCode'),
      minWidth: 120,
    },
    {
      field: 'productUnitName',
      title: $t('views.erp.common.productItem.unit'),
      minWidth: 80,
    },
    {
      field: 'remark',
      title: $t('common.remark'),
      minWidth: 150,
      slots: { default: 'remark' },
    },
    {
      field: 'count',
      title: $t('views.erp.common.productItem.count'),
      minWidth: 120,
      fixed: 'right',
      slots: { default: 'count' },
    },
    {
      field: 'productPrice',
      title: $t('views.erp.common.productItem.productPrice'),
      minWidth: 120,
      fixed: 'right',
      slots: { default: 'productPrice' },
    },
    {
      field: 'totalProductPrice',
      title: $t('views.erp.common.productItem.totalProductPrice'),
      minWidth: 130,
      fixed: 'right',
      formatter: 'formatAmount2',
    },
    {
      field: 'taxPercent',
      title: $t('views.erp.common.taxPercent'),
      minWidth: 110,
      fixed: 'right',
      slots: { default: 'taxPercent' },
    },
    {
      field: 'taxPrice',
      title: $t('views.erp.common.taxPrice'),
      minWidth: 120,
      fixed: 'right',
      formatter: 'formatAmount2',
    },
    {
      field: 'totalPrice',
      title: $t('views.erp.common.productItem.totalPrice'),
      minWidth: 120,
      fixed: 'right',
      formatter: 'formatAmount2',
    },
    {
      title: $t('common.action'),
      width: 80,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'no',
      label: $t('views.erp.sale.order.no'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.erp.sale.order.noPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'productId',
      label: $t('views.erp.common.product'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.productPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getProductSimpleList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
      },
    },
    {
      fieldName: 'orderTime',
      label: $t('views.erp.sale.order.orderTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
    {
      fieldName: 'supplierId',
      label: $t('views.erp.common.supplier'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.erp.common.supplierPlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getCustomerSimpleList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
      },
    },
    {
      fieldName: 'creator',
      label: $t('views.common.creatorName'),
      component: 'ApiSelect',
      componentProps: {
        placeholder: $t('views.common.creatorNamePlaceholder'),
        allowClear: true,
        showSearch: true,
        api: getSimpleUserList,
        fieldNames: {
          label: 'nickname',
          value: 'id',
        },
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.ERP_AUDIT_STATUS, 'number'),
        placeholder: $t('common.statusPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'remark',
      label: $t('common.remark'),
      component: 'Input',
      componentProps: {
        placeholder: $t('common.remarkPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'outStatus',
      label: $t('views.erp.sale.order.outStatus'),
      component: 'Select',
      componentProps: {
        options: [
          { label: '未出库', value: 0 },
          { label: '部分出库', value: 1 },
          { label: '全部出库', value: 2 },
        ],
        placeholder: $t('views.erp.sale.order.outStatusPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'returnStatus',
      label: $t('views.erp.sale.order.returnStatus'),
      component: 'Select',
      componentProps: {
        options: [
          { label: '未退货', value: 0 },
          { label: '部分退货', value: 1 },
          { label: '全部退货', value: 2 },
        ],
        placeholder: $t('views.erp.sale.order.returnStatusPlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      type: 'checkbox',
      width: 50,
      fixed: 'left',
    },
    {
      field: 'no',
      title: $t('views.erp.sale.order.no'),
      width: 200,
      fixed: 'left',
    },
    {
      field: 'productNames',
      title: $t('views.erp.sale.order.productNames'),
      showOverflow: 'tooltip',
      minWidth: 120,
    },
    {
      field: 'customerName',
      title: $t('views.erp.common.customer'),
      minWidth: 120,
    },
    {
      field: 'orderTime',
      title: $t('views.erp.sale.order.orderTime'),
      width: 160,
      formatter: 'formatDate',
    },
    {
      field: 'creatorName',
      title: $t('views.common.creatorName'),
      minWidth: 120,
    },
    {
      field: 'totalCount',
      title: $t('views.erp.sale.order.totalCount'),
      formatter: 'formatAmount3',
      minWidth: 120,
    },
    {
      field: 'outCount',
      title: $t('views.erp.sale.order.outCount'),
      formatter: 'formatAmount3',
      minWidth: 120,
    },
    {
      field: 'returnCount',
      title: $t('views.erp.sale.order.returnCount'),
      formatter: 'formatAmount3',
      minWidth: 120,
    },
    {
      field: 'totalProductPrice',
      title: $t('views.erp.sale.order.totalProductPrice'),
      formatter: 'formatAmount2',
      minWidth: 130,
    },
    {
      field: 'totalPrice',
      title: $t('views.erp.sale.order.totalPrice'),
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'depositPrice',
      title: $t('views.erp.sale.order.depositPrice'),
      formatter: 'formatAmount2',
      minWidth: 120,
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.ERP_AUDIT_STATUS },
      },
    },
    {
      title: $t('common.action'),
      width: 220,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
